<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="TLVBuffer.js"></script>

</head>
<body>

</body>
<script type="text/javascript">

    test();
    function test(){
        const JsEntity = {
            shortMin: 0,
            shortMax: 0,
            shortNull: null,
            intMin: 0,
            intMax: 0,
            intNull: null,
            longMin: 0,
            longMax: 0,
            longNull: null,
            floatMin: 0.0,
            floatMax: 0.0,
            floatNull: null,
            doubleMin:0.0,
            doubleMax:0.0,
            doubleNull:null,
            booleanTrue:false,
            booleanFalse:false,
            booleanNull:null,
            charMin:"",
            charMax:"",
            charNull:null,
            byteMin:"",
            byteMax:"",
            byteNull:null,

            bigDecimalMin:"",
            bigDecimalMax:"",
            bigDecimalNull:null,

            string1:"",
            string2:"",
            stringNull:null,

            date1:null,
            date2:null,

            localTime1:null,
            localTime2:null,

            localDate1:null,
            localDate2:null,

            localDateTime1:null,
            localDateTime2:null,
        };

        let buffer = new Uint8Array([104, 14, 57, 57, 57, 57, 57, 57, 46, 49, 50, 51, 52, 53, 54, 55, 104, 15, 45, 57, 57, 57, 57, 57, 57, 46, 49, 50, 51, 52, 53, 54, 55, 104, 0, 0, 1, 0, 0, 0, 0, 1, 1, 8, 1, 127, 8, 1, -128, 8, 0, 16, 3, -1, -1, 3, 16, 1, 0, 16, 0, 72, 6, -56, -88, -92, -63, -3, 49, 72, 0, 56, 9, -1, -1, -1, -1, -1, -1, -1, -9, 127, 56, 1, 1, 56, 0, 40, 5, -2, -56, -125, -78, 4, 40, 5, -88, -29, -48, -70, 12, 40, 0, 32, 5, -1, -1, -1, -1, 7, 32, 5, -128, -128, -128, -128, 8, 32, 0, 88, 6, -128, -8, -99, -92, -3, 49, 88, 0, 96, 6, -112, -24, -107, -65, -3, 49, 96, 0, 80, 6, -32, -86, -105, -71, -3, 49, 80, 0, 48, 9, -1, -1, -1, -1, -1, -1, -1, -1, 127, 48, 10, -128, -128, -128, -128, -128, -128, -128, -128, -128, 1, 48, 0, 24, 3, -1, -1, 1, 24, 5, -128, -128, -2, -1, 15, 24, 0, 64, 18, -28, -67, -96, -27, -91, -67, -27, -107, -118, 84, 76, 86, 66, 117, 102, 102, 101, 114, 64, 1, 48, 64, 0]);

        let tlv = new TLVBuffer();
        var object = tlv.deserialize(JsEntity,buffer);
        console.table(object);

        // 最小值
        const MIN_VALUE = '\u0000';
        // 最大值
        const MAX_VALUE = '\uFFFF';
        console.log(object.charMax === MAX_VALUE);
        console.log(object.charMin === MIN_VALUE);

    }

    function longTest(){
        let buffer = [48, 9, -1, -1, -1, -1, -1, -1, -1, -1, 127, 48, 10, -128, -128, -128, -128, -128, -128, -128, -128, -128, 1];
        const LongTest={
            min:0,
            max:0,
        }
        let tlv = new TLVBuffer();
        var object = tlv.deserialize(LongTest,buffer);
        console.table(object);
        console.log(object.min.toString());
        console.log(object.max.toString());
    }
    // test();




    // floatTest();
    function floatTest(){
        // let  buffer = new Uint8Array([40, 5, -1, -1, -1, -5, 7, 40, 1, 1]);
        let buffer = new Uint8Array([40, 5, -113, -64, -60, -65, 4, 40, 0]);
        const FloatTest={
            max:0,
            min:0,
        }
        let tlv = new TLVBuffer();
        var object = tlv.deserialize(FloatTest,buffer);
        console.table(object);
        console.log(object.max.toFixed(2) == 123456.12)
    }


    let ReflectUtil={
        getRootEntityArray:function (obj) {
            let filedArray = new Array();
            let keys = Object.keys(obj).sort();
            for (let key of keys) {
                if (obj.hasOwnProperty(key) ) {
                    let value = obj[key];
                    let type = typeof value;
                    filedArray.push({"field":obj,"key":key,"type":type});
                }
            }
            return filedArray;
        },
        fieldFilter:function (array,key){
            if (array){
                for (const element of array) {
                    if (element.key === key){
                        var fieldElement = element.field[key];
                        if (fieldElement instanceof Array){
                            return fieldElement[0];
                        }else if (fieldElement instanceof Object){
                            return fieldElement;
                        }
                    }
                }
            }
            return null;
        },
        getEntityKeys:function (entity,targetKey) {
            let filedArray = new Array();
            function parseObject(obj) {
                let keys = Object.keys(obj).sort();
                for (let key of keys) {
                    if (obj.hasOwnProperty(key) && key == targetKey) {

                        let value = obj[key];
                        let type = typeof value;

                        filedArray.push({"field":obj,"key":key,"type":type});
                        if (type === 'object' && value !== null) {
                            parseObject(value);
                        }
                    }
                }
            }

            function parseObjectByKey(obj,targetKey) {
                let parseObj = obj;
                if (typeof  obj == 'array'){
                    parseObj = obj[0];
                }
                let keys = Object.keys(parseObj).sort();
                for (let key of keys) {
                    if (parseObj.hasOwnProperty(key) ) {

                        let value = parseObj[key];
                        let type = typeof value;

                        // filedArray.push({"field":parseObj,"key":key,"type":type});
                        //
                        if (type === 'object' && value !== null) {
                            parseObjectChild(value);
                        }
                    }
                }
            }
            function parseObjectChild(obj) {
                let keys = Object.keys(obj).sort();
                for (let key of keys) {
                    if (obj.hasOwnProperty(key)) {

                        let value = obj[key];
                        let type = typeof value;

                        filedArray.push({"field":obj,"key":key,"type":type});

                        // if (type === 'object' && value !== null) {
                        //     parseObjectByKey(value,null);
                        // }
                    }
                }
            }
            parseObjectByKey(entity,targetKey);
            return filedArray;
        }
    }

    // arrayTest();
    function arrayTest(){
        let child= {
            abc:""
        }

        let arrayEntity = {
            childEntity:{child:child},
            childArray:[{child:child}]
        }

        // function parseEntity(entity) {
        //     function parseObject(obj, indent = '') {
        //         for (let key in obj) {
        //             if (obj.hasOwnProperty(key)) {
        //                 console.log(`${indent}Property: ${key}`);
        //                 let value = obj[key];
        //                 let type = typeof value;
        //                 console.log(`${indent}  Type: ${type}`);
        //
        //                 if (type === 'object' && value !== null) {
        //                     console.log(`${indent}  Value:`);
        //                     parseObject(value, indent + '    ');
        //                 } else {
        //                     console.log(`${indent}  Value: ${value}`);
        //                 }
        //             }
        //         }
        //     }
        //
        //     console.log('Parsing entity:');
        //     parseObject(entity);
        // }



        // let key1 = 'childArray';
        // let keys1 = getEntityKeys(arrayEntity, key1);
        // console.log(keys1);  // 输出: [{ key: "abc", type: "string" }]
        //
        // let key2 = 'childEntity';
        // let keys2 = getEntityKeys(arrayEntity, key2);
        // console.log(keys2);  // 输出: [{ key: "abc", type: "string" }]


        let userCourse={
            courseId:0,
            courseName:""
        }

        let userResponse={
            id:0,
            name:"",
            userCourse:userCourse,
        }

        let response={
            code:0,
            msg:"",
            data:{userResponse:userResponse}
        }


        var rootEntityArray = ReflectUtil.getRootEntityArray(response);
        // console.log("rootEntityArray:"+rootEntityArray);

        if (rootEntityArray){
            for (let element of rootEntityArray) {
                if (element.type === 'object'){
                    var keyObject = ReflectUtil.fieldFilter(rootEntityArray,element.key);
                    var entityKeys = ReflectUtil.getEntityKeys(keyObject);
                    console.log(entityKeys);
                    if (entityKeys){
                        for (let entityKey of entityKeys) {
                            if (entityKey.type == 'object'){
                                var keyObject2 = ReflectUtil.fieldFilter(entityKeys,entityKey.key);
                                var entityKeys2 = ReflectUtil.getEntityKeys(keyObject2);
                                console.log(entityKeys2)
                            }
                        }
                    }
                }
            }
        }



    }





</script>
</html>